package com.lagou;

import com.filter.TPMonitorFilter;
import com.lagou.bean.ConsumerComponent;
import com.lagou.bean.MethodInvokeTime;
import com.lagou.bean.UseTimeCompare;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        final ConsumerComponent service = context.getBean(ConsumerComponent.class);


        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                List<MethodInvokeTime> methodA = new LinkedList();
                methodA.addAll(TPMonitorFilter.getMethodAInvokeTimes());
                Collections.sort(methodA,new UseTimeCompare());
                int methodAtp90index = (int)Math.round(methodA.size()*0.9);
                int methodAtp99index = (int)Math.round(methodA.size()*0.99);
                long methodATP90 = methodA.get(methodAtp90index).getMethodUseTime();
                long methodATP99 = methodA.get(methodAtp99index).getMethodUseTime();

                List<MethodInvokeTime> methodB = new LinkedList();
                methodB.addAll(TPMonitorFilter.getMethodBInvokeTimes());
                Collections.sort(methodB,new UseTimeCompare());
                int methodBtp90index = (int)Math.round(methodB.size()*0.9);
                int methodBtp99index = (int)Math.round(methodB.size()*0.99);
                long methodBTP90 = methodB.get(methodBtp90index).getMethodUseTime();
                long methodBTP99 = methodB.get(methodBtp99index).getMethodUseTime();

                List<MethodInvokeTime> methodC = new LinkedList();
                methodC.addAll(TPMonitorFilter.getMethodCInvokeTimes());
                Collections.sort(methodC,new UseTimeCompare());
                int methodCtp90index = (int)Math.round(methodC.size()*0.9);
                int methodCtp99index = (int)Math.round(methodC.size()*0.99);
                long methodCTP90 = methodC.get(methodCtp90index).getMethodUseTime();
                long methodCTP99 = methodC.get(methodCtp99index).getMethodUseTime();

                System.out.println("methodA的tp90："+methodATP90+" tp99:"+methodATP99);
                System.out.println("methodB的tp90："+methodBTP90+" tp99:"+methodBTP99);
                System.out.println("methodC的tp90："+methodCTP90+" tp99:"+methodCTP99);
                System.out.println();
                System.out.println();
                System.out.println();
            }
        }, 5000, 5000);//在0秒后执行此任务,每次间隔5秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.


        // 创建一个可重用固定个数的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

        Runnable runnableA =  ()-> {
                String hello = null;
                try {
                    while (true){
                        hello = service.methodA("world", 0);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("result :" + hello);
            };
        Runnable runnableB = ()->{
                String hello = null;
                try {
                    while (true){
                        hello = service.methodB("world", 0);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("result :" + hello);
        };
        Runnable runnableC = ()->{
                String hello = null;
                try {
                    while (true){
                        hello = service.methodC("world", 0);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                System.out.println("result :" + hello);
        };



            fixedThreadPool.execute(runnableA);
            fixedThreadPool.execute(runnableB);
            fixedThreadPool.execute(runnableC);




    }




    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
